package com.example.demo.controller;

import com.example.demo.entity.Music;
import com.example.demo.entity.NoteCreate;
import com.example.demo.service.NoteCreateService;
import com.example.demo.util.FileUtil;
import com.example.demo.vo.ViewNoteCommentInfo;
import com.example.demo.vo.ViewNoteCreateInfo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("v1")
public class NoteCreateController {
    @Autowired
    private NoteCreateService noteCreateService;
    // 获取按浏览量排序的热门笔记
    @GetMapping("/NoteCreate/view_count")
    public List<NoteCreate> getNoteCreateByview_count() {
        return noteCreateService.findNoteCreateByview_count();
    }

    //笔记信息与用户信息相关联
    @GetMapping("/NoteCreateinfo")
    public PageInfo<ViewNoteCreateInfo> findNoteCreateInfostatus(@RequestParam Integer pageNum, @RequestParam Integer pageSize,@RequestParam String note_status){
        PageInfo<ViewNoteCreateInfo> pageInfo = noteCreateService.findNoteCreateInfostatus(pageNum,pageSize,note_status);
        return pageInfo;
    }

    //笔记信息与用户信息相关联（不分页）,获取用户所有信息
    @GetMapping("/NoteCreateinfonoteid")
    public PageInfo<ViewNoteCreateInfo> getPageNoteCreateInfouserid(@RequestParam Integer note_create_id) {
        PageInfo<ViewNoteCreateInfo> pageInfo = noteCreateService.findNoteCreateUserId(note_create_id);
        return pageInfo;
    }



    //笔记表和用户表关联：获取用户名以及用户头像
    @GetMapping("/NoteCreateinfouser")
    public PageInfo<ViewNoteCreateInfo> getPageNoteCreateInfouser() {
        PageInfo<ViewNoteCreateInfo> pageInfo = noteCreateService.findNoteCreateInfouser();
        return pageInfo;
    }


    @GetMapping("/NoteCreate/pages")
    public PageInfo<NoteCreate> getPageNoteCreate(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        return noteCreateService.findNoteCreatePages(pageNum, pageSize);
    }

    //修改状态
    @PutMapping("/{id}/status")
    public Integer updateNoteStatus(
            @PathVariable("id") Integer note_create_id,
            @RequestBody String note_status) {
        return noteCreateService.updateNoteStatus(note_create_id, note_status);
    }

    //根据笔记id查询数据
    @GetMapping("/NoteCreateinfo/{id}")
    public ViewNoteCreateInfo getPageNoteCreateInfoid(@PathVariable Integer id) {
        return noteCreateService.findNoteCreateinfoDyId(id);
    }

    //根据用户id和笔记状态查找
    @GetMapping("/NoteCreateinfo1")
    public PageInfo<ViewNoteCreateInfo> getPageNoteCreateInfo(
                                                              @RequestParam String note_status,
                                                              @RequestParam Integer user_id) {
        PageInfo<ViewNoteCreateInfo> pageInfo = noteCreateService.findNoteCreateInfo(note_status, user_id);
        return pageInfo;
    }

    // 根据笔记状态和用户ID统计笔记数量
    @GetMapping("/NoteCreate/count")
    public ResponseEntity<Integer> countNoteByStatusAndUser(
            @RequestParam String note_status,
            @RequestParam Integer user_id) {
            Integer count = noteCreateService.countPublishedNotesByStatus(note_status, user_id);
            return ResponseEntity.ok(count);
    }

    @GetMapping("/NoteCreate")
    public List<NoteCreate> getNoteCreate() {
        return noteCreateService.findNoteCreateAll();
    }
    //修改审核通过状态
    @PutMapping("/NoteCreate/statusyes")
    public Integer updateNoteStatusyes(@RequestBody NoteCreate noteCreate) {
        return noteCreateService.updateNoteStatusyes(noteCreate);
    }

    //修改审核未通过状态
    @PutMapping("/NoteCreate/statusno")
    public Integer updateNoteStatusno(@RequestBody NoteCreate noteCreate) {
        return noteCreateService.updateNoteStatusno(noteCreate);
    }

    //笔记状态待审核
    @PutMapping("/NoteCreate/statusReview")
    public Integer updateNoteStatusReview(@RequestBody NoteCreate noteCreate) {
        return noteCreateService.updateNoteStatusReview(noteCreate);
    }


    @GetMapping("/NoteCreate/{id}")
    public NoteCreate getNoteCreateId(@PathVariable Integer id) {
        return noteCreateService.findNoteCreateDyId(id);
    }

    //增加笔记
    @PostMapping("/NoteCreate")
    public Integer addNoteCreate(@RequestBody NoteCreate noteCreate) {
        System.out.println(noteCreate);
        return noteCreateService.insertNoteCreate(noteCreate);
    }

    //修改点赞量
    @PutMapping("/NoteCreate")
    public Integer updateNoteCreate(@RequestBody NoteCreate noteCreate) {
        System.out.println(noteCreate);
        return noteCreateService.updateNoteCreate(noteCreate);
    }

    //修改笔记内容
    @PutMapping("/NoteCreateEdit")
    public Integer updateNoteCreate1(@RequestBody NoteCreate noteCreate) {
        System.out.println(noteCreate);
        return noteCreateService.updateNoteCreate1(noteCreate);
    }

    @DeleteMapping("/NoteCreate/{id}")
    public Integer deleteNoteCreate(@PathVariable("id") Integer note_create_id) {
        return noteCreateService.deleteNoteCreate(note_create_id);
    }

    //点击量
    @PostMapping("/NoteCreate/View/{note_create_id}")
    public ResponseEntity<?> ViewNote(@PathVariable Integer note_create_id) {
        noteCreateService.incrementViewCount(note_create_id);
        return ResponseEntity.ok().build();
    }

    //上传图片
    @PostMapping("/NoteCreate/upload")
    public String getPicFileName(MultipartFile file){
        String oldFileName = file.getOriginalFilename();
        System.out.println(oldFileName);
        String typeName = oldFileName.substring(oldFileName.lastIndexOf('.'));

        String filePath = FileUtil.getUploadFilePath();
        System.out.println(typeName);
        String newFileName = System.currentTimeMillis()+typeName;

        try {
            FileUtil.uploadFile(file.getBytes(),filePath,newFileName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return newFileName;
    }
}

